package com.ruoyi.config

import com.baomidou.shaun.core.mgt.ProfileStateManager
import com.baomidou.shaun.core.profile.TokenProfile
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Configuration
import org.springframework.data.redis.core.StringRedisTemplate

@Configuration
class AiPicProfileStateManager : ProfileStateManager {

    private val log = LoggerFactory.getLogger(AiPicProfileStateManager::class.java)

    @Autowired
    private lateinit var stringRedisTemplate: StringRedisTemplate


    val userTokenKey: (String) -> String = {
        "user:token:$it"
    }

    override fun online(profile: TokenProfile) {
        log.info("token放入到redis中")
        stringRedisTemplate.opsForValue().set(userTokenKey(profile.id.toString()), profile.token)
    }

    override fun offline(profile: TokenProfile) {
        log.info("移除${profile.id}的token信息")
        stringRedisTemplate.delete(userTokenKey(profile.id.toString()))
    }

    override fun isOnline(profile: TokenProfile): Boolean {

        log.info("当前profile是 $profile")
        return stringRedisTemplate.opsForValue().get(userTokenKey(profile.id.toString())) == profile.token
    }
}